﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TfL
{
    //[Serializable]
    [XmlType("S")]
    public class TrainTimesForStation
    {
        public TrainTimesForStation(string line)
        {
            _line = line;
            Code = string.Empty;
            Name = string.Empty;
            Platforms = new List<Platform>();

        }
        [XmlAttribute("Code")]
        public string Code { get; set; }

        [XmlAttribute("N")]
        public string Name { get; set; }
        
        [XmlArray("")]
        public List<Platform> Platforms { get; set; }

        public string Line
        {
            get { return _line; }
        }

        public void Parse(XmlReader reader)
        {
            Code = reader.GetAttribute("Code");
            Name = reader.GetAttribute("N");
            while (reader.Read())
            {
                if (reader.IsEmptyElement)
                    continue;
                if (reader.NodeType == XmlNodeType.EndElement)
                    break;
                if (reader.Name == "P")
                {
                    Platform platform = new Platform();
                    platform.Parse(reader);
                    Platforms.Add(platform);
                }
            }
        }

        public override string ToString()
        {
            return string.Format("{0}({1}): {2} platforms \n\r\t\t {3}", Name,Line, Platforms.Count,Platforms.Aggregate("",(seed,platform)=> seed + "\n\r\t\t"+platform));
        }

        private readonly string _line;
    }
}
